package com.zr.repertory.affiliation.service.impl;

import com.zr.repertory.affiliation.mapper.LegalPersonMapper;
import com.zr.repertory.affiliation.mapper.WareHouseBelongMapper;
import com.zr.repertory.affiliation.mapper.WareLocationMapper;
import com.zr.repertory.affiliation.model.pojo.*;
import com.zr.repertory.affiliation.service.WareHouseBelongService;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName WareHouseBelongServiceImpl
 * @Description TODO
 * @Author Jiakui_Ma
 * @Date 2019/11/21 11:36
 */
@Service
public class WareHouseBelongServiceImpl implements WareHouseBelongService {
    @Autowired
    private WareHouseBelongMapper wareHouseBelongMapper;
    @Autowired
    private LegalPersonMapper legalPersonMapper;
    @Autowired
    private WareLocationMapper wareLocationMapper;
    @Autowired
    private PageUtil<WareHouseBelong> page;

    /**
     * 分页查询
     * @param belongPageVo
     * @return
     */
    @Override
    public PageUtil<WareHouseBelong> queryPage(BelongPageVo belongPageVo) {

        page.setPageIndex(belongPageVo.getPageIndex());
        page.setTotalNumber(wareHouseBelongMapper.getTotalCount(belongPageVo));
        page.setPageSize(belongPageVo.getPageSize());

        System.out.println(belongPageVo);
        belongPageVo.setIndex(page.getIndex());
        page.setDataList(wareHouseBelongMapper.getCurrentPageData(belongPageVo));
        return page;

    }

    /**
     * 查看
     * @param id
     * @return
     */
    @Override
    public BelongSeeResultVo see(Integer id) {

        BelongSeeResultVo belongSeeResultVo =  wareHouseBelongMapper.queryById(id);

        return belongSeeResultVo;

    }

    /**
     * 添加
     * @param addBelongVo
     * @return
     */
    @Transactional
    @Override
    public ResultUtil add(AddBelongVo addBelongVo) {

        //验证法人是否存在
        LegalPerson legalPerson = legalPersonMapper.queryById(addBelongVo.getLegalPersonId());
        if (legalPerson == null) {
            return ResultBuilderUtil.error("法人不存在");
        }
        //验证库位是否存在
        int count  = wareLocationMapper.queryLocation(addBelongVo.getCompanyStorageBinValueList());
        if (count<=0) {
            return ResultBuilderUtil.error("库位不存在");
        }
        //删除数据
        wareHouseBelongMapper.delete(addBelongVo.getLegalPersonId());
        //入库
        wareHouseBelongMapper.add(addBelongVo);
        return ResultBuilderUtil.success(null);
    }
}
